package com.stars.services.impl.company;
import com.github.pagehelper.PageInfo;
import com.stars.common.SpecialSymbolUtils;
import com.stars.common.base.dto.BaseTreeDto;
import com.stars.entity.company.CompanyEntity;
import com.stars.services.company.CompanyAccessServices;
import com.stars.dao.mapper.company.CompanyMapper;
import com.stars.entity.dto.company.CompanyDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.stars.entity.ListPage;
import com.github.pagehelper.PageHelper;
import com.stars.services.impl.BaseAccessServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @Title:  业务逻辑
 * @author dj
 * @date 2019-09-30 13:00:05
 * @version V1.0
 *
 */
@Service
public class CompanyAccessServicesImpl  extends BaseAccessServiceImpl<CompanyMapper , CompanyEntity> implements  CompanyAccessServices {
    @Autowired
    private CompanyMapper  companyMapper;
    /**
     * 分页查询
     * @param pageNum 查询参数 起始页，大小。
     * @return
     */
    @Override
    public PageInfo<CompanyDTO> pageList(Integer pageNum, Integer pageSize, CompanyDTO command) throws Exception{
        ListPage<CompanyDTO> listPage = new ListPage<>(pageNum, pageSize, command);
        PageHelper.startPage(pageNum, pageSize);
        List<CompanyDTO> list = companyMapper.pageQueryCompany(listPage);
        PageInfo<CompanyDTO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<CompanyDTO> findTree(CompanyDTO companyDTO) throws Exception {
        List<CompanyEntity> companyEntities = new ArrayList<>();
        if(StringUtils.isEmpty(companyDTO.getCompanyName())){
            if(StringUtils.isEmpty(companyDTO.getId())){
                companyDTO.setId("root");
            }
            CompanyEntity CompanyEntity = companyMapper.selectByPrimaryKey(companyDTO.getId());
            companyEntities.add(CompanyEntity);
        }else {
            companyEntities = companyMapper.findByName(SpecialSymbolUtils
                    .convert(companyDTO.getCompanyName()));
        }
        List<CompanyDTO> list = companyMapper.findTree(companyEntities);

        return CompanyDTO.getTree(list);
    }

}